<?php

//session_start();
include 'autoload.php';

use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\HttpClients\FacebookHttpable;

class Library_Facebook_Base {

    private $url = 'http://haohao.dev/';

    public function __construct() {
        $config_facebook = Kohana::$config->load('facebook');
        FacebookSession::setDefaultApplication($config_facebook->appId, $config_facebook->secret);
    }

    public function getSession() {
        return new FacebookSession('access-token-here');
    }

    public function getUser() {
        $data = array('ok' => false, 'user' => null, 'urllogin');
        $helper = new FacebookRedirectLoginHelper($this->url);

        try {
            $session = $helper->getSessionFromRedirect();
        } catch (FacebookRequestException $ex) {
            // When Facebook returns an error
        } catch (Exception $ex) {
            // When validation fails or other local issues
        }

        if (isset($session)) {
            // graph api request for user data
            $request = new FacebookRequest($session, 'GET', '/me');
            $response = $request->execute();
            // get response
            $graphObject = $response->getGraphObject();

            // print data
            $data['ok'] = true;
            $data['user'] = $graphObject->asArray();
            $request = new FacebookRequest(
                    $session, 'GET', '/me/picture?type=large&redirect=false'
            );
            $response = $request->execute();
            $graphObject = $response->getGraphObject()->asArray();
            $data['user']['avatar'] = "";
            if(!empty($graphObject['url'])){
                $data['user']['avatar'] = $graphObject['url'];
            }
            
        } else {
            $data['urllogin'] = $helper->getLoginUrl() . "user_about_me%2Cemail%2Cuser_status%2Cpublish_actions%2Cuser_friends";
        }


        return $data;
    }

    public function getPicture() {
        $data = array('ok' => false, 'picture' => null, 'urllogin');
        $helper = new FacebookRedirectLoginHelper($this->url);

        try {
            $session = $helper->getSessionFromRedirect();
        } catch (FacebookRequestException $ex) {
            // When Facebook returns an error
        } catch (Exception $ex) {
            // When validation fails or other local issues
        }

        if (isset($session)) {
            // graph api request for user data
            $request = new FacebookRequest(
                    $session, 'GET', '/me/picture'
            );

            $response = $request->execute();
            $graphObject = $response->getGraphObject();

            print_r($response);
            die;
            // get response
            $graphObject = $response->getGraphObject();

            // print data
            $data['ok'] = true;
            $data['user'] = $graphObject->asArray();
        } else {
            $data['urllogin'] = $helper->getLoginUrl() . "user_about_me%2Cemail%2Cuser_status%2Cpublish_actions%2Cuser_friends";
        }


        return $data;
    }

}

?>
